<template>
  <div class="updateImg">
    <el-upload
      class="avatar-uploader"
      action="#"
      :show-file-list="false"
      :before-upload="beforeAvatarUpload"
      :http-request="httpRequest"
    >
      <img
        v-if="value && show"
        :src="value"
        class="avatar"
        @click.stop="isShow = true"
      >
      <i v-else class="el-icon-plus avatar-uploader-icon" />
    </el-upload>
    <i v-if="value" class="close" @click="closeEvent">X</i>
    <!-- 图片预览 -->
    <el-dialog :visible.sync="isShow" width="500px">
      <img style="width: 100%" :src="value" alt="">
    </el-dialog>
    <transition name="fade">
      <el-progress
        v-if="!show"
        class="progress"
        type="circle"
        :percentage="percentage"
        status="success"
      />
    </transition>
  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5'
const cos = new COS({
  SecretId: 'AKID7C8oENGsEJ9hyyjCOJxKRHq9BwQebHgo',
  SecretKey: 'uXtxlHWUBbkcEjzyg7HAm8D0WljQqzsU'
})
export default {
  name: 'UpdateImg',
  props: {
    value: { type: String, default: '' }
  },
  data() {
    return {
      isShow: false,
      show: true,
      percentage: 0
    }
  },
  methods: {
    beforeAvatarUpload(file) {
      const isJPG =
        file.type === 'image/png' ||
        file.type === 'image/jpeg' ||
        file.type === 'image/gif'
      const isLt2M = file.size / 1024 / 1024 < 5

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    httpRequest(data) {
      console.log(data)
      this.percentage = 0
      this.show = false
      cos.putObject(
        {
          Bucket: 'gc-1308138799', // 存储桶
          Region: 'ap-guangzhou', // 地域
          Key: data.file.uid + '', // 唯一标识
          Body: data.file, // 要上传的文件对象
          StorageClass: 'STANDARD', // 上传的模式类型 直接默认 标准模式即可
          // 上传到腾讯云 =》 哪个存储桶 哪个地域的存储桶 文件  格式  名称 回调
          onProgress: (progressData) => {
            // 上传进度，实时返回上传文件进度
            console.log(JSON.stringify(progressData))
            this.percentage = progressData.percent * 100
            if ((progressData.percent = 1)) {
              setTimeout(() => {
                this.show = true
              }, 1500)
            }
          }
        },
        (err, data) => {
          // data返回数据之后 应该如何处理
          console.log(err || data)
          if (!err) {
            this.$emit('input', 'http://' + data.Location)
          }
        }
      )
    },
    // 关闭当前图片
    closeEvent() {
      this.$emit('input', '')
    }
  }
}
</script>

<style lang="scss" scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.updateImg {
  width: 178px;
  height: 178px;
  border: 1px dashed #ccc;
  position: relative;
  &:hover {
    border: 1px dashed red;
    .close {
      display: block;
    }
  }
  .close {
    display: none;
    position: absolute;
    top: 10px;
    right: 10px;
    cursor: pointer;
  }
  .progress {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 999999;
  }
}
.fade-enter-active,
.fade-leave-active {
  transition: all 0.5;
}
.fade-enter,
.fade-leave-to {
  opacity: 0;
}
</style>
